package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.CouponDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.model.Coupon;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.CouponService;
import com.yami.shop.service.ProductService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/coupon/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private ProductService productService;

    /**
     * 分页获取
     */
//    @GetMapping("/page")
////    @PreAuthorize("@pms.hasPermission('coupon:coupon:page')")
//    public ResponseEntity<IPage<Coupon>> page(Coupon coupon, PageParam<Coupon> page){
//        IPage<Coupon> couponList = couponService.page(page,new LambdaQueryWrapper<Coupon>()
//                .eq(Coupon::getShopId, SecurityUtils.getSysUser().getShopId())
//                .orderByDesc(Coupon::getCouponId));
//        return ResponseEntity.ok(couponList);
//    }

    @GetMapping("/page")
    public ResponseEntity<IPage<CouponDto>> prodList(
            Coupon coupon,PageParam<CouponDto> page) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        coupon.setShopId(shopId);
        IPage<CouponDto> CouponDtoIPage = couponService.pageByProdId(page, coupon);
        return ResponseEntity.ok(CouponDtoIPage);
    }


    /**
     * 优惠券商品获取
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/couponShop")
    public ResponseEntity<List<Product>> couponShop() {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        List<Product> couponProduct = productService.getCouponShop(shopId);
        return ResponseEntity.ok(couponProduct);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:info')")
    public ResponseEntity<Coupon> info(@PathVariable("id") Long id){
        Coupon coupon = couponService.getById(id);
        return ResponseEntity.ok(coupon);
    }

    /**
     * 保存
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:save')")
    public ResponseEntity<Void> save(@Valid @RequestBody Coupon coupon) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long shopId = SecurityUtils.getSysUser().getShopId();
        Long userId = SecurityUtils.getSysUser().getUserId();
        coupon.setShopId(shopId);
        coupon.setUserId(userId);
        coupon.setState(0);
//        coupon.setCouponKind(0);
        coupon.setCreateTime(new Date());
        String start = coupon.getStartTime();
        String startTime = null;
        startTime = sdf.format(sdf.parse(start));
        coupon.setStartTime(startTime);
        String end = coupon.getEndTime();
        String endTime = sdf.format(sdf.parse(end));
        coupon.setEndTime(endTime);
        couponService.save(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:update')")
    public ResponseEntity<Void> update(@Valid @RequestBody Coupon coupon) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        coupon.setUpdateTime(new Date());
        String start = coupon.getStartTime();
        String startTime = null;
        startTime = sdf.format(sdf.parse(start));
        coupon.setStartTime(startTime);
        String end = coupon.getEndTime();
        String endTime = sdf.format(sdf.parse(end));
        coupon.setEndTime(endTime);
        couponService.updateById(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:delete')")
    public ResponseEntity<Void> delete(@RequestBody Long[] ids){
        couponService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok().build();
    }

    /**
     * 查询优惠券领取使用详情
     * @param coupon
     * @param page
     * @return
     */
    @GetMapping("/couponUsePage")
    public ResponseEntity<IPage<CouponDto>> couponUsePage(
            Coupon coupon,PageParam<CouponDto> page) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        coupon.setShopId(shopId);
        IPage<CouponDto> couponUsePage = couponService.couponUseByProdId(page, coupon);
        return ResponseEntity.ok(couponUsePage);
    }

}
